//
//  GoodsTableView.m
//  AppleCilent
//
//  Created by Nail Sharipov on 05/04/14.
//  Copyright (c) 2014 Nail Sharipov. All rights reserved.
//

#import "GoodsTableView.h"
#import "GoodsTableViewCell.h"
#import "MainProperties.h"
#import "ResourceData.h"
#import "GoodsDetailsViewController.h"
#import "SceneManager.h"
#import <QuartzCore/QuartzCore.h>


@implementation GoodsTableView
{
    NSArray* goodsList;
    MenuDomain* menu;
    NSString* imageFolderPath;
}

- (void) setMenu:(MenuDomain *)menuDomain
{
    menu = menuDomain;
    goodsList = [menu getGoods];
    [self reloadData];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
  
    return self;
}


- (id)initWithCoder:(NSCoder*)coder
{
    if ((self = [super initWithCoder:coder])) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    self.backgroundView = nil;
    self.dataSource = self;
    self.delegate = self;
    [self setEditing:NO animated:NO];
    [self setSeparatorColor: [UIColor greenColor]];
    
    CALayer* layer = [self  layer];
    [layer setCornerRadius:10.0f];
    [layer setMasksToBounds:YES];
    [layer setBorderColor:[UIColor yellowColor].CGColor];
    [layer setBorderWidth:1.0f];
    
    imageFolderPath = [[MainProperties getInstance] getImageFolder];
}


-(UITableViewCell*)tableView: (UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cellId";
    
    GoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if(cell == nil) {
        cell = [[GoodsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    if (indexPath.row < [goodsList count]) {
        GoodsDomain* goods = (GoodsDomain*)[goodsList objectAtIndex:indexPath.row];
        [cell setGoods:goods];
        [[cell name] setText:[goods getName]];
        [[cell description] setText:[goods getDescription]];

        NSString* price = [NSString stringWithFormat: @"%iр.", (int)[goods getPrice]];
        [[cell price] setText:price];
//        [cell.description sizeToFit];
        NSString* imageName = [goods getImage];
        NSString* imagePath = [NSString stringWithFormat:@"%@/%@", imageFolderPath, imageName];
        UIImage* image = [UIImage imageWithContentsOfFile:imagePath];
        [[cell image] setImage:image];
    }
    
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    return cell;
}

- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
    
    GoodsTableViewCell* cell = (GoodsTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    
    GoodsDomain* goods = [cell getGoods];
    
    GoodsDetailsViewController* viewController = (GoodsDetailsViewController*)[[SceneManager getInstance] openScene: @"GoodsDetails"];

    [viewController setGoods:goods];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [GoodsTableViewCell getHeight];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [goodsList count];
}



@end
